﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace GenericStateMachine.Test
{
    [TestClass]
    public class StateContextBaseTest
    {
        [TestMethod]
        public void ActionTest()
        {
            Debug.WriteLine("<< new TestService()");
            var service = new TestService();
            Assert.AreEqual(TestService.ServiceState.Created, service.AsDynamic().stateMachine.CurrentState);
            Assert.AreEqual(false, service.AsDynamic().stateMachine.IsHandling);
            Debug.WriteLine(">> new TestService()");

            Debug.WriteLine(string.Empty);

            Assert.AreEqual(null, service.Parameter);

            service.Action0();
            Assert.AreEqual(Unit.Default, service.Parameter);

            service.Action1(0);
            Assert.AreEqual(Tuple.Create<byte>(0), service.Parameter);

            service.Action2(1, 2);
            Assert.AreEqual(Tuple.Create<byte, short>(1, 2), service.Parameter);

            service.Action3(3, 4, 5);
            Assert.AreEqual(Tuple.Create<byte, short, int>(3, 4, 5), service.Parameter);

            service.Action4(6, 7, 8, 9);
            Assert.AreEqual(Tuple.Create<byte, short, int, long>(6, 7, 8, 9), service.Parameter);

            Debug.WriteLine(string.Empty);

            Debug.WriteLine("<< TestService.Stop");
            service.Stop();
            Assert.AreEqual(TestService.ServiceState.Stopped, service.AsDynamic().stateMachine.CurrentState);
            Debug.WriteLine(">> TestService.Stop");
        }

        [TestMethod]
        public void FuncTest()
        {
            string result;

            Debug.WriteLine("<< new TestService()");
            var service = new TestService();
            Assert.AreEqual(TestService.ServiceState.Created, service.AsDynamic().stateMachine.CurrentState);
            Assert.AreEqual(false, service.AsDynamic().stateMachine.IsHandling);
            Debug.WriteLine(">> new TestService()");

            Debug.WriteLine(string.Empty);

            Assert.AreEqual(null, service.Parameter);

            result = service.Func0();
            Assert.AreEqual("Func0", result);
            Assert.AreEqual(Unit.Default, service.Parameter);

            result = service.Func1(9);
            Assert.AreEqual("Func1", result);
            Assert.AreEqual(Tuple.Create<long>(9), service.Parameter);

            result = service.Func2(8, 7);
            Assert.AreEqual("Func2", result);
            Assert.AreEqual(Tuple.Create<long, int>(8, 7), service.Parameter);

            result = service.Func3(6, 5, 4);
            Assert.AreEqual("Func3", result);
            Assert.AreEqual(Tuple.Create<long, int, short>(6, 5, 4), service.Parameter);

            result = service.Func4(3, 2, 1, 0);
            Assert.AreEqual("Func4", result);
            Assert.AreEqual(Tuple.Create<long, int, short, byte>(3, 2, 1, 0), service.Parameter);

            Debug.WriteLine(string.Empty);

            Debug.WriteLine("<< TestService.Stop");
            service.Stop();
            Assert.AreEqual(TestService.ServiceState.Stopped, service.AsDynamic().stateMachine.CurrentState);
            Debug.WriteLine(">> TestService.Stop");
        }
    }
}
